﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Simulacion.NumberGenerator
{
    /// <summary>
    /// Number generator.
    /// </summary>
    class Generator
    {
        private static double _m;
        private static double _a;
        private static double _X;
        private static double _c;
        private static double _seed;
        public static List<double> RegNumberList = new List<double>(); //para guardar registro de los numeros generados

        static Generator() //generador estandar
        {
            _X = 100;
            _seed = 100;
            _a = 16807;
            _m = 2147483647;
            _c = 0;
        }

        public static double Seed
        {
            get { return Generator._seed; }
            set { Generator._X = value; Generator._seed = value; }
        }

        public static double M
        {
            get { return Generator._m; }
            set { Generator._m = value; }
        }

        public static double A
        {
            get { return Generator._a; }
            set { Generator._a = value; }
        }

        public static double C
        {
            get { return Generator._c; }
            set { Generator._c = value; }
        }

        public static double NextNumber
        {
            get
            {
                _X = (_a * _X + _c) % _m;
                double number = _X / _m;
                RegNumberList.Add(number);
                return number;
            }
        }

        public static double[] getNumberList(int quantity)
        {
            double[] nums = new double[quantity];
            for (int i = 0; i < quantity; i++)
                nums[i] = NextNumber;

            return nums;
        }

        public static double pruebaFrecuencia(int inter, double[] numrecs)
        {
            //fe = nos dice la frecuencia esperada
            //l, in = toman los valores de longitud y de intervalos
            //X0 = estadistico
            double fe, l = numrecs.Length, X0 = 0;
            //frecuencia observada
            int[] frecuency = new int[inter];
            //se calcula la frecuencia esperada
            fe = l / inter;
            //se calculan las frecuencias observadas
            for (int i = 0; i < l; i++)
            {
                for (double j = 1; j <= inter; j++)
                {
                    if (numrecs[i] <= (j / inter))
                    {
                        frecuency[(int)j - 1]++;
                        break;
                    }
                }
            }
            //se calcula el estadistico
            for (int i = 0; i < inter; i++)
            {
                X0 = X0 + (1 / fe) * Math.Pow(frecuency[i] - fe, 2);
            }
            return X0;
        }
    }
}
